package com.ha.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ha.dto.GarbageDTO;
import com.ha.dto.GarbagePageDTO;
import com.ha.entity.Garbage;
import com.ha.entity.GarbageKind;
import com.ha.exception.BaseException;
import com.ha.exception.GarbageKindRepeatException;
import com.ha.mapper.GarbageKindMapper;
import com.ha.mapper.GarbageMapper;
import com.ha.result.PageResult;
import com.ha.service.GarbageService;
import com.ha.vo.GarbageKindVO;
import com.ha.vo.GarbageVO;
import com.ha.vo.KindIdVO;
import com.ha.vo.UserGarbageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: GarbageServiceImpl
 * Package: com.ha.service.impl
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/4/21  14:03
 * @Version 1.0
 */
@Service
public class GarbageServiceImpl implements GarbageService {


    @Autowired
    private GarbageMapper garbageMapper;
    @Autowired
    private GarbageKindMapper garbageKindMapper;

    @Override
    public PageResult pageQuery(GarbagePageDTO garbagePageDTO) {
        //开始分页查询,动态添加limit 和计算 来实现查询,基于拦截器
        PageHelper.startPage(garbagePageDTO.getPage(),garbagePageDTO.getPageSize());
        Page<GarbageVO> page = garbageMapper.pageQuery(garbagePageDTO);
        List<KindIdVO> kindVOList = garbageKindMapper.getKindVOList();
        HashMap<Long, String> map = new HashMap<>();
        for (KindIdVO kindIdVO : kindVOList) {
            map.put(kindIdVO.getId(),kindIdVO.getKind());
        }
        long total = page.getTotal();
        List<GarbageVO> result = page.getResult();
        for (GarbageVO vo : result) {
            vo.setKind(map.get(vo.getRubbishKindId()));
        }
        return new PageResult(total,result);
    }

    @Override
    public GarbageVO getById(Long id) {
        Garbage garbage = garbageMapper.getById(id);
        GarbageVO garbageVO = new GarbageVO();
        BeanUtils.copyProperties(garbage,garbageVO);

        garbageVO.setKind(garbageKindMapper.getById(garbage.getRubbishKindId()).getKind());
        return garbageVO;
    }

    @Override
    public void update(GarbageDTO garbageDTO) {

        if(garbageDTO != null ){
            Garbage garbage = Garbage.builder()
                    .id(garbageDTO.getId())
                    .image(garbageDTO.getImage())
                    .introduction(garbageDTO.getIntroduction())
                    .name(garbageDTO.getName())
                    .rubbishKindId(garbageDTO.getRubbishKindId())
                    .build();
           Garbage garbage1 =  garbageMapper.getByName(garbage.getName());
           if(garbage1 != null && garbage1.getName() == garbage.getName()){
               throw new GarbageKindRepeatException("该垃圾名称已存在");

           }else {
               if(garbageKindMapper.getById(garbage.getRubbishKindId()) != null){
                   garbageMapper.update(garbage);
               }else{
                   throw new GarbageKindRepeatException("不存在该垃圾种类");
               }

           }
        }



    }

    @Override
    public void deleteById(Long id) {
        garbageMapper.deleteById(id);
    }

    @Override
    public void addGarbage(GarbageDTO garbageDTO) {
        if(garbageDTO != null ){
            Garbage garbage = Garbage.builder()
                    .id(garbageDTO.getId())
                    .image(garbageDTO.getImage())
                    .name(garbageDTO.getName())
                    .introduction(garbageDTO.getIntroduction())
                    .rubbishKindId(garbageDTO.getRubbishKindId())
                    .build();
            Garbage garbage1 =  garbageMapper.getByName(garbage.getName());
            if(garbage1 != null){
                throw new GarbageKindRepeatException("该垃圾名称已存在");
            }else {
                if(garbageKindMapper.getById(garbage.getRubbishKindId())!=null){
                    garbageMapper.addGarbage(garbage);
                }else {
                    throw new GarbageKindRepeatException("不存在该垃圾种类");
                }

            }
        }
    }

    @Override
    public List<UserGarbageVO> getByName(String name) {
        if(name != null && name != ""){
            List<UserGarbageVO> byName = garbageKindMapper.getByName(name);

            List<KindIdVO> kindVOList = garbageKindMapper.getKindVOList();
            HashMap<Long, String> map = new HashMap<>();
            for (KindIdVO kindIdVO : kindVOList) {
                map.put(kindIdVO.getId(),kindIdVO.getKind());
            }
            for (UserGarbageVO vo : byName) {
                vo.setKind(map.get(vo.getId()));
            }
            return byName;
        }else {
            throw new BaseException("未查询到该垃圾");
        }
    }
}
